// pages/giftCard/selectGiftCard.js

var app = getApp()
var requests = require('../../requests/api.js');
import util from '../../utils/util.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {

    prompt: {
      hidden: !0,
      icon: '../../assets/coupons/default-coupons.png',
      title: '没有礼品卡哦~',
    },
    giftCardArray: [],
    orderAmount: 0.0,
    selectArray: [],
    screenHeight: wx.getSystemInfoSync().windowHeight
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    new app.WeToast();
    if (options.orderAmount) {
      this.setData({
        "orderAmount": parseFloat(options.orderAmount)

      })
      this.my_giftCards(JSON.parse(options.selectArray));

    }

  },

  //我的优惠券列表
  my_giftCards: function(selectArray) {
    const self = this;

    requests.getGiftCardAvailable((res) => {
      console.log(res)
      var giftCardArr = res.data.giftCardList;
      if (selectArray.length > 0) {
        for (var i = 0; i < selectArray.length; i++) {
          for (var j = 0; j < giftCardArr.length; j++) {
            if (giftCardArr[j].giftCardId == selectArray[i].giftCardId) {
              giftCardArr[j].isSelect = true
            }
          }
        }
      }
      self.setData({
        giftCardArray: giftCardArr, //给数组赋值，

      })

    }, (data) => {

      if (util.alertNetworkError(data)) {
        self.wetoast.toast({
          title: data.resMsg,
        })
      }
    }, () => {
      this.setData({
        'prompt.hidden': this.data.giftCardArray.length,
      })
    });
  },

  //优惠券 点击事件
  onGiftCardClick: function(event) {
    // if (event.currentTarget.dataset.coupon.status == 1) {
    console.log(event)
    var giftCardArr = this.data.giftCardArray
    for (var i = 0; i < giftCardArr.length; i++) {
      if (giftCardArr[i].giftCardId == event.currentTarget.dataset.coupon.giftCardId) {
        giftCardArr[i].isSelect = !event.currentTarget.dataset.coupon.isSelect
        this.setData({
          giftCardArray: giftCardArr
        })
      }
    }

  },

  //上拉触底
  moreData: function() {},

  /**
   * 当页面滑动到底部时触发的方法,删掉这个方法后控制台会报事件警告
   */
  onReachBottom: function() {},

  sureClick: function() {
    var giftCardArr = this.data.giftCardArray
    var selectArray = []
    var amount = 0
    for (var i = 0; i < giftCardArr.length; i++) {
      if (giftCardArr[i].isSelect) {
        amount += parseFloat(giftCardArr[i].balance)
        selectArray.push(giftCardArr[i])
      }
    }
    if (amount > 0) {
      var giftCardAmount = parseFloat(0)
      var isOverFlow = false
      var remainingAmount = parseFloat(this.data.orderAmount)
      for (var i = 0; i < selectArray.length; i++) {
        var balance = parseFloat(selectArray[i].balance)
        giftCardAmount += balance
        if (giftCardAmount >= parseFloat(this.data.orderAmount) && i + 1 < selectArray.length) {
          isOverFlow = true
          break
        }
        //如果选中礼品卡，计算剩余的金额是否大于礼品卡的金额，如果大于礼品卡的金额，则使用金额为礼品卡全部的金额，如果小于的话，使用的金额为剩余金额
        if (remainingAmount > balance) {
          remainingAmount = remainingAmount - balance
          selectArray[i].giftCardAmount = balance
        } else {
          selectArray[i].giftCardAmount = remainingAmount
        }
      }
      if (isOverFlow) {
        this.wetoast.toast({
          title: '礼品卡金额大于商品金额',
        })
        return
      }
      console.log(selectArray)
      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 2]
      prevPage.setGiftCard(selectArray)
      wx.navigateBack()

    } else {
      this.wetoast.toast({
        title: '请选择礼品卡',
      })
    }
  },
  // 购买新卡
  buyClick: function() {
    wx.navigateTo({
      url: '../giftCard/giftCardProduct',
    })
  }

})